Shallow Copy vs. Deep Copy β
Ever wondered how to clone objects in JavaScript without pulling your hair out? π€ Well, you're in luck! Today, weβre unpacking the quirks of shallow and deep copying. Buckle up as we explore these essential techniques to avoid the dreaded βunexpected mutationβ fiasco! π
Shallow Copy: The Surface Dweller πββοΈ β
A shallow copy is like that friend who only skims the headlines and thinks they know everything. It copies only the top-level elements of an object, leaving nested objects to fend for themselves.
const original = {
name: "Alice",
address: {
city: "Wonderland",
zip: "12345"
}
};
const shallowCopy = { ...original };
shallowCopy.name = "Bob"; //Changes only the shallowCopy
shallowCopy.address.city = "New York"; // Changes both original and shallowCopy
console.log(original.name); // Alice (unchanged)
console.log(original.address.city); // New York (changed)π± Wait, what just happened? You changed shallowCopy.address.city, and BAM, original.address.city is also changed. Because our friend shallow copy here just copied the reference to address. It's like moving out but keeping the keys to your old place. π π
Methods for Shallow Copy: β
- Spread operator (
...) β the cool kid π Object.assign()β the classic gentleman π©Array.prototype.slice()for arrays β the slicer πArray.from()for arrays β the formal inviter π§
Deep Copy: The Deep Diver πββοΈ β
Now, letβs talk about deep copy, the one who actually reads the whole article (or object) and makes a complete, independent copy of everything. Nested objects? No problem. Deep copy has got it all covered.
const original = {
name: "Alice",
address: {
city: "Wonderland",
zip: "12345"
}
};
const deepCopy = JSON.parse(JSON.stringify(original));
deepCopy.name = "Bob";
deepCopy.address.city = "New York";
console.log(original.name); // Alice (unchanged)
console.log(original.address.city); // Wonderland (unchanged)π₯³ Now thatβs more like it! deepCopy changes do not affect the original object at all. Itβs like moving to a new house and actually handing over your old keys. π‘πͺ
Methods for Deep Copy: β
JSON.parse(JSON.stringify())β the fast but sometimes flaky friend π (note: doesnβt handle functions,undefined, or other non-serializable values)- Libraries like Lodash (
_.cloneDeep()) β the reliable and trustworthy buddy π€ - Custom recursive function β the DIY enthusiast π οΈ
When to Use Which? β
- Shallow Copy: Use this when youβre in a hurry and just need a quick clone of the top-level properties. Perfect for those βquick fixesβ that always turn into βlong-term problemsβ later. π
- Deep Copy: When you need a complete, independent replica, deep copy is your go-to. Ideal for when you donβt want your changes to mess with the original data, especially for complex objects.
π Pro Tip β
π¨ Be cautious with JSON.parse(JSON.stringify()) for large or complex objects. It can be inefficient and sometimes just plain quirky, especially with special object types like Date, Map, Set, and custom objects. π§ββοΈ
